package net.akkz.server.impl;

import java.util.ArrayList;

import net.akkz.dao.ImgViewDao;
import net.akkz.dao.ImgViewGoodDao;
import net.akkz.dbFactory.DbManage;
import net.akkz.dbFactory.Session;
import net.akkz.model.ImgView;
import net.akkz.model.ImgViewGood;
import net.akkz.server.ImgViewServer;

public class ImgViewServerImpl implements ImgViewServer
{
	private ImgViewDao imgViewDao;
	private ImgViewGoodDao imgViewGoodDao;
	private DbManage dbManage;

	@Override
	public void addImgView(ImgView imgView)
	{
		Session session = dbManage.createSession();

		try
		{
			imgViewDao.addImgView(session, imgView);

			session.commit();
		}
		catch (Exception e)
		{
			session.rollback();
		}
	}

	@Override
	public ArrayList<ImgView> findImgView(int start, int end)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<ImgView> imgViews = imgViewDao.findImgView(session, start, end);

			session.commit();

			return imgViews;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public ArrayList<ImgView> findImgViewOrderbyGoodNum(int start, int end)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<ImgView> imgViews = imgViewDao.findImgViewOrderByGoodNum(session, start, end);

			session.commit();

			return imgViews;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public ArrayList<ImgView> findImgViewByStudentId(int studentId)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<ImgView> imgViews = imgViewDao.findImgViewByStudentId(session, studentId);

			session.commit();

			return imgViews;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public ArrayList<ImgView> findAllImgView(int start, int end)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<ImgView> imgViews = imgViewDao.findAllImgView(session, start, end);

			session.commit();

			return imgViews;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public ArrayList<ImgView> findAllImgViewOrderbyGoodNum(int start, int end)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<ImgView> imgViews = imgViewDao.findAllImgViewOrderByGoodNum(session, start, end);

			session.commit();

			return imgViews;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public ArrayList<ImgView> findAllImgViewByStudentId(int studentId)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<ImgView> imgViews = imgViewDao.findAllImgViewByStudentId(session, studentId);

			session.commit();

			return imgViews;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public ImgView getImgView(int imgViewid)
	{
		Session session = dbManage.createSession();

		try
		{
			ImgView imgView = imgViewDao.getImgView(session, imgViewid);
			imgView.setState(1002);

			session.commit();

			return imgView;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}
	
	@Override
	public ImgView getImgView(int imgViewid, int studentid)
	{
		Session session = dbManage.createSession();

		try
		{
			ImgView imgView = imgViewDao.getImgView(session, imgViewid);
			
			ImgViewGood imgViewGood = new ImgViewGood();
			imgViewGood.setImgviewid(imgViewid);
			imgViewGood.setStudentid(studentid);
			
			if(imgViewGoodDao.checkImgViewGood(session, imgViewGood))
			{
				imgView.setState(1001);
			}
			else
			{
				imgView.setState(1000);
			}

			session.commit();

			return imgView;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public void deleteImgView(int imgViewId)
	{
		Session session = dbManage.createSession();

		try
		{
			imgViewDao.deleteImgView(session, imgViewId);

			session.commit();
		}
		catch (Exception e)
		{
			session.rollback();
		}
	}

	@Override
	public void checkImgView(ImgView imgView)
	{
		Session session = dbManage.createSession();

		try
		{
			imgView.setState(1);

			imgViewDao.checkImgViewGood(session, imgView);

			session.commit();
		}
		catch (Exception e)
		{
			session.rollback();
		}
	}

	@Override
	public void addImgViewGood(int imgViewGoodId, int studentId)
	{
		Session session = dbManage.createSession();

		try
		{
			ImgView imgView = imgViewDao.getImgView(session, imgViewGoodId);
			imgView.setGoodnum(imgView.getGoodnum() + 1);

			ImgViewGood imgViewGood = new ImgViewGood();
			imgViewGood.setImgviewid(imgViewGoodId);
			imgViewGood.setStudentid(studentId);

			imgViewGoodDao.addImgViewGood(session, imgViewGood);
			imgViewDao.updateImgViewGood(session, imgView);

			session.commit();
		}
		catch (Exception e)
		{
			session.rollback();
		}

	}

	@Override
	public void unAddImgViewGood(int imgViewGoodId, int studentId)
	{
		Session session = dbManage.createSession();

		try
		{
			ImgViewGood imgViewGood = new ImgViewGood();
			imgViewGood.setImgviewid(imgViewGoodId);
			imgViewGood.setStudentid(studentId);
			
			if(imgViewGoodDao.checkImgViewGood(session, imgViewGood))
			{
				ImgView imgView = imgViewDao.getImgView(session, imgViewGoodId);
				imgView.setGoodnum(imgView.getGoodnum() - 1);
				
				imgViewGoodDao.deleteImgViewId(session, imgViewGood);
				imgViewDao.updateImgViewGood(session, imgView);
			}

			session.commit();
		}
		catch (Exception e)
		{
			session.rollback();
		}

	}

	@Override
	public ArrayList<ImgViewGood> findImgViewGoodByImgViewId(int imgViewId)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<ImgViewGood> imgViewGoods = imgViewGoodDao.findImgViewByImgViewId(session, imgViewId);

			session.commit();

			return imgViewGoods;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public ArrayList<ImgViewGood> findImgViewGoodByStudentId(int studentId)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<ImgViewGood> imgViewGoods = imgViewGoodDao.findImgViewByStudentId(session, studentId);

			session.commit();

			return imgViewGoods;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	public void setImgViewDao(ImgViewDao imgViewDao)
	{
		this.imgViewDao = imgViewDao;
	}

	public void setImgViewGoodDao(ImgViewGoodDao imgViewGoodDao)
	{
		this.imgViewGoodDao = imgViewGoodDao;
	}

	public void setDbManage(DbManage dbManage)
	{
		this.dbManage = dbManage;
	}

}
